package database;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

public class HelpSelect {
    public JScrollPane scpDemo;
    public JTableHeader jth;
    public JTable tabDemo;
    Room room;
    public HelpSelect(int select){
        switch (select){
            case 1:{
                init(1);
                break;
            }
            case 2:{
                init(2);
                break;
            }
            case 3:{
                init(3);
                break;
            }
            case 4:{
                init(4);
                break;
            }
        }

    }
    public void init(int select1){
        JFrame f = new JFrame("用户二级界面");
        f.setLayout(new FlowLayout());
        f.setSize(600,700);
        f.setLocation(450,20);
        if(select1==4) {
            f.setLayout(new GridLayout(6,1,10,5));
            JPanel jp1 = new JPanel();
            JLabel jl1 = new JLabel("请点击按钮");
            JButton jb1 = new JButton("寝室查询");
            jp1.add(jl1);
            jp1.add(jb1);
            f.add(jp1);
            JPanel jp2 =new JPanel();
            JLabel jl2 = new JLabel("请点击按钮");
            JButton jb2 = new JButton("寝室长查询");
            jp2.add(jl2);
            jp2.add(jb2);
            f.add(jp2);
            JPanel jp5 = new JPanel();
            JLabel jl5 = new JLabel("请输入你的要求");
            JTextField jt = new JTextField("",15);
            jp5.add(jl5);
            jp5.add(jt);
            f.add(jp5);
            JPanel jp3 = new JPanel();
            JLabel jl3 = new JLabel("请点击按钮");
            JButton jb3 = new JButton("寝室卫生排名表");
            jp3.add(jl3);
            jp3.add(jb3);
            f.add(jp3);
            JPanel jp4 = new JPanel();
            JLabel jl4 = new JLabel("请点击按钮");
            JButton jb4 = new JButton("查询整个学校寝室数据");
            jp4.add(jl4);
            jp4.add(jb4);
            f.add(jp4);

            scpDemo = new JScrollPane();
            f.add(scpDemo);
            jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int number_ = Integer.parseInt(jt.getText());
                    Room room1 = new Room();
                    room1.setRnum(number_);
                    Manu manu =new Manu(1,room1);
                    room = manu.getJjxyroom();
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(room);
                    btnShow_ActionPerformed(e,arrayList);
                }
            });
            jb2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String studentname_ = jt.getText();
                    Room room1 = new Room();
                    room1.setStudentname(studentname_);
                    Manu manu =new Manu(2,room1);
                    room = manu.getJjxyroom();
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(room);
                    btnShow_ActionPerformed(e,arrayList);
                }
            });
            jb3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Room room1 = new Room();
                    Manu manu =new Manu(3,room1);
                    ArrayList arrayList = new ArrayList();
                    arrayList = manu.getArrayList();
                    btnShow_ActionPerformed2(e,arrayList);
                }
            });
            jb4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Room room = new Room();
                    Manu manu=new Manu(7,room);
                    ArrayList arrayList = new ArrayList();
                    arrayList = manu.getArrayList();
                    btnShow_ActionPerformed3(e,arrayList);
                }
            });

        }
        if(select1==2){
            f.setLayout(new GridLayout(2,1,10,5));
            JPanel button = new JPanel();
            JLabel button_ = new JLabel("请点击按钮");
            JButton jb1 = new JButton("删除寝室");
            button.add(button_);
            button.add(jb1);
            f.add(button);//1
            JPanel r_num = new JPanel();
            JLabel roomnum = new JLabel("删除的寝室号码：");
            JTextField rnum = new JTextField("",15);
            r_num.add(roomnum);
            r_num.add(rnum);
            f.add(r_num);//2
            jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Room room = new Room();
                    int rnum1 = Integer.parseInt(rnum.getText());
                    room.setRnum(rnum1);
                    new Manu(6,room);
                }
            });
        }
        if(select1==3){
            f.setLayout(new GridLayout(8,1,10,5));
            JPanel button = new JPanel();
            JLabel button_ = new JLabel("请点击按钮");
            JButton jb1 = new JButton("修改寝室");
            button.add(button_);
            button.add(jb1);
            f.add(button);//1
            JPanel r_name = new JPanel();
            JLabel roomname = new JLabel("新的寝室名字：");
            JTextField rname = new JTextField("",15);
            r_name.add(roomname);
            r_name.add(rname);
            f.add(r_name);//2
            JPanel r_num = new JPanel();
            JLabel roomnum = new JLabel("寝室号码：");
            JTextField rnum = new JTextField("请输入原来的寝室号码",15);
            r_num.add(roomnum);
            r_num.add(rnum);
            f.add(r_num);//3
            JPanel r_count = new JPanel();
            JLabel roomcount = new JLabel("寝室人数：");
            JTextField rcount = new JTextField("",15);
            r_count.add(roomcount);
            r_count.add(rcount);
            f.add(r_count);//4
            JPanel r_sex = new JPanel();
            JLabel roomsex = new JLabel("寝室成员性别：");
            JTextField rsex = new JTextField("",15);
            r_sex.add(roomsex);
            r_sex.add(rsex);
            f.add(r_sex);//5
            JPanel r_sanitate = new JPanel();
            JLabel roomsanitate = new JLabel("寝室卫生：");
            JTextField rsanitate = new JTextField("",15);
            r_sanitate.add(roomsanitate);
            r_sanitate.add(rsanitate);
            f.add(r_sanitate);//6
            JPanel r_award = new JPanel();
            JLabel roomaward = new JLabel("寝室获奖情况：");
            JTextField raward = new JTextField("",15);
            r_award.add(roomaward);
            r_award.add(raward);
            f.add(r_award);//7
            JPanel r_studentname = new JPanel();
            JLabel roomstudentname = new JLabel("寝室长姓名：");
            JTextField rstudentname = new JTextField("",15);
            r_studentname.add(roomstudentname);
            r_studentname.add(rstudentname);
            f.add(r_studentname);//8
            jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name1 = rname.getText();
                    int rnum1 = Integer.parseInt(rnum.getText());
                    int count1 = Integer.parseInt(rcount.getText());
                    String sex1 = rsex.getText();
                    String sanitate1 = rsanitate.getText();
                    String award1 = raward.getText();
                    String studentname1 = rstudentname.getText();
                    Room room = new Room(name1,rnum1,count1,award1,sex1,sanitate1,studentname1);
                    new Manu(5,room);
                }
            });
        }
        if(select1==1){
            f.setLayout(new GridLayout(8,1,10,5));
            JPanel button = new JPanel();
            JLabel button_ = new JLabel("请点击按钮");
            JButton jb1 = new JButton("添加寝室");
            button.add(button_);
            button.add(jb1);
            f.add(button);//1
            JPanel r_name = new JPanel();
            JLabel roomname = new JLabel("寝室名字：");
            JTextField rname = new JTextField("",15);
            r_name.add(roomname);
            r_name.add(rname);
            f.add(r_name);//2
            JPanel r_num = new JPanel();
            JLabel roomnum = new JLabel("寝室号码：");
            JTextField rnum = new JTextField("",15);
            r_num.add(roomnum);
            r_num.add(rnum);
            f.add(r_num);//3
            JPanel r_count = new JPanel();
            JLabel roomcount = new JLabel("寝室人数：");
            JTextField rcount = new JTextField("",15);
            r_count.add(roomcount);
            r_count.add(rcount);
            f.add(r_count);//4
            JPanel r_sex = new JPanel();
            JLabel roomsex = new JLabel("寝室成员性别：");
            JTextField rsex = new JTextField("",15);
            r_sex.add(roomsex);
            r_sex.add(rsex);
            f.add(r_sex);//5
            JPanel r_sanitate = new JPanel();
            JLabel roomsanitate = new JLabel("寝室卫生：");
            JTextField rsanitate = new JTextField("",15);
            r_sanitate.add(roomsanitate);
            r_sanitate.add(rsanitate);
            f.add(r_sanitate);//6
            JPanel r_award = new JPanel();
            JLabel roomaward = new JLabel("寝室获奖情况：");
            JTextField raward = new JTextField("",15);
            r_award.add(roomaward);
            r_award.add(raward);
            f.add(r_award);//7
            JPanel r_studentname = new JPanel();
            JLabel roomstudentname = new JLabel("寝室长姓名：");
            JTextField rstudentname = new JTextField("",15);
            r_studentname.add(roomstudentname);
            r_studentname.add(rstudentname);
            f.add(r_studentname);//8

            jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //new Manu(4);

                    String name1 = rname.getText();
                    int rnum1 = Integer.parseInt(rnum.getText());
                    int count1 = Integer.parseInt(rcount.getText());
                    String sex1 = rsex.getText();
                    String sanitate1 = rsanitate.getText();
                    String award1 = raward.getText();
                    String studentname1 = rstudentname.getText();
                    Room room = new Room(name1,rnum1,count1,award1,sex1,sanitate1,studentname1);
                    new Manu(4,room);
                }
            });
        }
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    public void btnShow_ActionPerformed(ActionEvent ae,ArrayList arr){
        String[] title = {"寝室名字","寝室号码","寝室人数","寝室性别","寝室卫生","寝室获奖情况","寝室长姓名"};
        Object[][] info = new Object[1][7];
        room = (Room) arr.get(0);
        info[0][0] = room.getName();
        info[0][1] = room.getRnum();
        info[0][2] = room.getCount();
        info[0][3] = room.getSex();
        info[0][4] = room.getSanitate();
        info[0][5] = room.getAward();
        info[0][6] = room.getStudentname();
        this.tabDemo = new JTable(info, title);
        // 显示表头
        this.jth = this.tabDemo.getTableHeader();
        // 将JTable加入到带滚动条的面板中
        this.scpDemo.getViewport().add(tabDemo);
    }
    public void btnShow_ActionPerformed2(ActionEvent e , ArrayList arrayList){
        String[] title = {"寝室卫生排名（匿名）"};
        Object[][] info = new Object[arrayList.size()][1];
        int count = 0;
        for(Object obj:arrayList){
            info[count][0] = (String)obj;
            count++;
        }
        this.tabDemo = new JTable(info, title);
        // 显示表头
        this.jth = this.tabDemo.getTableHeader();
        // 将JTable加入到带滚动条的面板中
        this.scpDemo.getViewport().add(tabDemo);
    }
    public void btnShow_ActionPerformed3(ActionEvent e,ArrayList arr){
        String[] title = {"寝室名字","寝室号码","寝室人数","寝室性别","寝室卫生","寝室获奖情况","寝室长姓名"};
        Object[][] info = new Object[arr.size()][7];
        int count=0;
        Iterator iterator = arr.iterator();
        while (iterator.hasNext()){
            Room room1 =(Room) iterator.next();
            info[count][0] = room1.getName();
            info[count][1] = room1.getRnum();
            info[count][2] = room1.getCount();
            info[count][3] = room1.getSex();
            info[count][4] = room1.getSanitate();
            info[count][5] = room1.getAward();
            info[count][6] = room1.getStudentname();
            count++;
        }
        this.tabDemo = new JTable(info, title);
        // 显示表头
        this.jth = this.tabDemo.getTableHeader();
        // 将JTable加入到带滚动条的面板中
        this.scpDemo.getViewport().add(tabDemo);
    }
}
